
package com.wk.code.wkserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.code.wkserver.entity.Permission;
import com.wk.code.wkserver.entity.SysUser;
import com.wk.code.wkserver.exception.BizException;
import com.wk.code.wkserver.mapper.PermissionMapper;
import com.wk.code.wkserver.mapper.RoleMapper;
import com.wk.code.wkserver.mapper.SysUserMapper;
import com.wk.code.wkserver.request.admin.PermissionRequest;
import com.wk.code.wkserver.response.admin.MenuResponse;
import com.wk.code.wkserver.response.admin.PermissionResponse;
import com.wk.code.wkserver.response.admin.PermissionTreeResponse;
import com.wk.code.wkserver.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 权限服务层
 *
 * @author wq
 * @date 2021-07-12 14:42:10
 */
@Service("permissionService")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<MenuResponse> findMenus(Integer roleId) {
        List<Permission> permissions = baseMapper.selectByRole(roleId);
        return combination(permissions, 0);
    }

    private List<MenuResponse> combination(List<Permission> permissions, Integer parentId) {
        List<MenuResponse> vos = permissions.stream()
                .filter(i -> i.getParentId().equals(parentId))
                .map(this::convertMenuVo)
                .collect(Collectors.toList());
        for (MenuResponse vo : vos) {
            List<MenuResponse> children = combination(permissions, vo.getId());
            vo.setChildren(children);
        }
        return vos;
    }

    @Override
    public void addPermission(PermissionRequest req) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(req, permission);
        save(permission);
    }

    @Override
    public void updatePermission(Integer id, PermissionRequest req) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(req, permission);
        permission.setParentId(null);
        permission.setId(id);
        updateById(permission);
    }

    @Override
    public void delPermission(Integer id) {
        Boolean child = baseMapper.selectHaveChild(id);
        if (child) {
            throw new BizException("");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<PermissionTreeResponse> findTree() {
        List<Permission> permissions = baseMapper.selectAll();
        return combinationTree(permissions, 0);
    }

    @Override
    public PermissionResponse detail(Integer id) {
        Permission permission = getById(id);
        if (Objects.isNull(permission)) {
            throw new BizException("该权限已被删除");
        }
        PermissionResponse response = new PermissionResponse();
        BeanUtils.copyProperties(permission, response);
        return response;
    }

    @Override
    public List<MenuResponse> getMenus(Integer userId) {
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (Objects.nonNull(sysUser)) {
            return this.findMenus(sysUser.getRoleId());
        }
        return null;
    }

    private List<PermissionTreeResponse> combinationTree(List<Permission> permissions, Integer parentId) {
        List<PermissionTreeResponse> vos = permissions.stream()
                .filter(i -> i.getParentId().equals(parentId))
                .map(this::convertPermissionTreeVo)
                .collect(Collectors.toList());
        for (PermissionTreeResponse vo : vos) {
            List<PermissionTreeResponse> children = combinationTree(permissions, vo.getId());
            if (children.size() > 0) {
                vo.setChildren(children);
            }
        }
        return vos;
    }

    private MenuResponse convertMenuVo(Permission permission) {
        MenuResponse vo = new MenuResponse();
        vo.setId(permission.getId());
        vo.setTitle(permission.getTitle());
        vo.setPath(permission.getPath());
        vo.setIcon(permission.getIcon());
        return vo;
    }

    private PermissionTreeResponse convertPermissionTreeVo(Permission permission) {
        PermissionTreeResponse vo = new PermissionTreeResponse();
        vo.setId(permission.getId());
        vo.setTitle(permission.getTitle());
        return vo;
    }
}
